using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Business.Dto;
using JTZY.Model.Business;
using JTZY.Repository;
using JTZY.Service.Business.IBusinessService;

namespace JTZY.Service.Business
{
    /// <summary>
    /// 业务员列表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ISaleManService), ServiceLifetime = LifeTime.Transient)]
    public class SaleManService : BaseService<SaleMan>, ISaleManService
    {
        /// <summary>
        /// 查询业务员列表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<SaleManDto> GetList(SaleManQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<SaleMan, SaleManDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public SaleMan GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加业务员列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public SaleMan AddSaleMan(SaleMan model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改业务员列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateSaleMan(SaleMan model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<SaleMan> QueryExp(SaleManQueryDto parm)
        {
            var predicate = Expressionable.Create<SaleMan>();
            predicate = predicate.AndIF(parm.UserId.HasValue, it => it.AgentId == parm.UserId);
            return predicate;
        }
    }
}